/**
 * 获取日期集合 初始化日历总数据
 * @export
 * @param {*} Year
 * @param {*} Month
 * @param {*} weekStart 设置为1表示周一为一周的开始
 * @return {*}
 */
export default function layout(Year, Month, weekStart = 1) {
  if (weekStart == 0) {
    weekStart = 7;
  }
  if (Month < 10) {
    Month = '0' + Month;
  }
  // 获取当前日期
  let currentDate = new Date().getDate();
  // 获取当前月第一天是星期几
  let currentMonthFirstDayWeek = new Date(Year, Month - 1, 1).getDay();
  if (currentMonthFirstDayWeek === 0) {
    currentMonthFirstDayWeek = 7;
  }
  // 获取当前月最后一天天数
  let currentMonthLastDay = new Date(new Date(Year, Month, 1) - 86400 * 1000).getDate();
  // 获取上个月最后一天天数
  let prevMonthLastDay = new Date(new Date(Year, Month - 1, 1) - 86400 * 1000).getDate();

  let timeData = [];

  let prevDayLen = currentMonthFirstDayWeek - weekStart;
  if (prevDayLen < 0) {
    prevDayLen = 7 + prevDayLen;
  }
  for (let i = 0; i < prevDayLen; i++) {
    timeData.push({
      time: '',
      isActive: false
    });
  }
  // for (let i = prevMonthLastDay - (currentMonthFirstDayWeek - 1) + 1; i <= prevMonthLastDay; i++) {
  //   // timeData.push(`${Month == 1 ? Year - 1 : Year}-${Month == 1 ? 12 : Month - 1}-${i}`);
  //   timeData.push({
  //     time: '',
  //     isActive: false
  //   });
  // }
  for (let i = 1; i <= currentMonthLastDay; i++) {
    timeData.push({
      time: `${Year}-${Month}-${i < 10 ? '0' + i : i}`,
      isActive: false
    });
  }
  let suppleDate = 42 - currentMonthLastDay - (currentMonthFirstDayWeek - 1);
  for (let i = 1; i <= suppleDate; i++) {
    // timeData.push(`${Month == 12 ? Year + 1 : Year}-${Month == 12 ? 1 : Month + 1}-${i}`);
    timeData.push({
      time: '',
      isActive: false
    });
  }
  return timeData;
}

/**
 * 根据日期获取当前是第几周
 * @export
 * @param {*} str
 * @return {*}
 */
export function getWeek(str) {
  let day = Date.parse(str);
  //如果不是当年的第一天不是星期一，则该日所属周数为上一年的最后一周
  day = new Date(day);

  if (day.getDay() !== 1) {
    day = day.getTime() - 24 * 60 * 60 * 1000;
    day = new Date(day);
  }
  day.setMonth(0);
  day.setDate(1);
  day.setHours(0);
  day.setMinutes(0);
  day.setSeconds(0); //到这里就得到该年的一月一日

  let today = Date.parse(str);
  today = new Date(today);
  let todayWeek = today.getDay();

  //计算日期是一年中的第几天
  let rankDay = Math.ceil((today.getTime() - day.getTime()) / (1000 * 24 * 60 * 60));
  let rankWeek = Math.ceil(rankDay / 7);
  rankWeek = rankWeek < 10 ? rankWeek : rankWeek;
  let year = day.getFullYear().toString();
  return rankWeek;
}
/**
 * 获取周集合
 * @export
 * @param {*} data
 * @return {*}
 */
export function getWeekArray(data) {
  let newRes = [];
  let res = data.map((t) => t.time);
  for (let i = 0; i < res.length; i++) {
    for (let j = 0; j < res[i].length; j++) {
      if (j % 7 == 0) {
        // 获取每行第一天
        if (!res[i][j].time) {
          // 一整行都是空的 传null
          if (res[i][j + 6].time == '') {
            newRes.push(null);
          } else {
            if (i > 0) {
              if (res[i - 1][35].time) {
                newRes.push(getWeek(res[i - 1][35].time));
              } else if (res[i - 1][28].time) {
                newRes.push(getWeek(res[i - 1][28].time));
              } else if (res[i - 1][21].time) {
                newRes.push(getWeek(res[i - 1][28].time));
              }
            } else {
              for (let k = 0; k < 6; k++) {
                if (res[0][k].time) {
                  newRes.push(getWeek(res[0][k].time));
                }
              }
            }
          }
        } else {
          newRes.push(getWeek(res[i][j].time));
        }
      }
    }
  }
  return getNewArray(newRes, 6);
}

/**
 *数组切分
 * @param {*} array
 * @param {*} subGroupLength
 * @return {*}
 */
function getNewArray(array, subGroupLength) {
  let index = 0;
  let newArray = [];
  while (index < array.length) {
    newArray.push(array.slice(index, (index += subGroupLength)));
  }
  return newArray;
}
